package ucadminlogic

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"go.uber.org/zap"
	"navi-mumbai/app/rpc/usercenter/internal/svc"
	"navi-mumbai/app/rpc/usercenter/pb"
	"navi-mumbai/common/key/dbkey"
	"navi-mumbai/common/xerr"
)

type QueryUserInviteConfigLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewQueryUserInviteConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QueryUserInviteConfigLogic {
	return &QueryUserInviteConfigLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 创建UserInviteConfig设置
func (l *QueryUserInviteConfigLogic) QueryUserInviteConfig(in *pb.QueryUserInviteConfigReq) (*pb.QueryUserInviteConfigResp, error) {
	l.Logger.Errorf("QueryUserInviteConfig in :  ", in)
	resp, err := l.svcCtx.UserInviteConfigModel.Find(l.ctx)
	if err != nil {
		l.Logger.Errorf("QueryUserInviteConfigReq Find failed ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}
	r := &pb.QueryUserInviteConfigResp{
		List: make([]*pb.QueryUserInviteConfigData, 0),
	}
	for _, d := range resp {
		queryData := &pb.QueryUserInviteConfigData{
			Id:                     d.Id, // 序号
			SuperiorLevel:          d.SuperiorLevel,
			ReturnRatio:            d.ReturnRatio,
			ReturnNewRegisterMoney: d.ReturnNewRegisterMoney,
			RechargeLimit:          d.RechargeLimit,
			RechargeReturnMoney:    d.RechargeReturnMoney,
			Link:                   d.Link,
			CreateTime:             d.CreateTime.Format(dbkey.DateTimeCNFormat),
			UpdateTime:             d.UpdateTime.Format(dbkey.DateTimeCNFormat),
			Enable:                 d.Enable,
		}
		r.List = append(r.List, queryData)
	}
	if err != nil {
		l.Logger.Errorf("QueryUserInviteConfigReq Count failed ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}
	return r, nil
}
